package com.example.jetpacktest.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import com.example.jetpacktest.databinding.ActivityTest002Binding
import com.example.jetpacktest.lifecycle.LifecycleTest001

/**
 * 测试 LifecycleOwner
 */
class Test002Activity : Activity(), LifecycleOwner {

    private val TAG: String = this.javaClass.simpleName

    private lateinit var lifecycle: LifecycleRegistry

    private lateinit var lifeCycleTest001: LifecycleTest001

    private lateinit var binding: ActivityTest002Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTest002Binding.inflate(layoutInflater)
        setContentView(binding.root)
        lifecycle = LifecycleRegistry(this)
        lifeCycleTest001 = LifecycleTest001()
        lifecycle.addObserver(lifeCycleTest001) //添加生命周期观察者
        initMy()
    }

    private fun initMy(){
        binding.btn1.setOnClickListener{
            val intent = Intent(this, Test001Activity::class.java)
            startActivity(intent)
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "Test002Activity........onStart()")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "Test002Activity........onResume()")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "Test002Activity........onPause()")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "Test002Activity........onStop()")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "Test002Activity........onDestroy()")
    }

    override fun getLifecycle(): Lifecycle {
        return lifecycle
    }

}